package com.itheima.反射reflect.myreflect2;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

public class ReflectDemo2 {
    public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {

        Class clazz = Class.forName("com.itheima.反射reflect.myreflect2.Student2");
        //获取所有的public修饰的构造方法
        Constructor[] constructors = clazz.getConstructors();
        //获取所有的public/private修饰的构造方法
        Constructor[] declaredConstructors = clazz.getDeclaredConstructors();
        //获取指定的public的修饰的构造方法
        Constructor constructor1 = clazz.getConstructor();
        Constructor constructor2 = clazz.getConstructor(String.class, int.class);
     //   Constructor constructor3 = clazz.getConstructor(int.class); //没有就报错
        Constructor constructor = clazz.getDeclaredConstructor(String.class);

        Student2 stu1 = (Student2) constructor2.newInstance("zhangsan", 23);


    }
}

class Student2 {
    private String name;
    private int age;

    private Student2(String name){
        System.out.println("name的值为："+name);
        System.out.println("private..Student..有参构造方法");

    }

    public Student2() {
        System.out.println("oublic..Student...无参构造方法");
    }

    public Student2(String name, int age) {
        System.out.println("name的值为："+name+"age的值为："+age);
        System.out.println("public..Student..有参构造方法");
    }
}
